var module = angular.module('app.StorepayController', []);

module.controller('StorepayController',function ($scope, $location) {
    // $location.path('/storepay/index');
    $scope.$emit('hideNav',true);

});


module.controller('StorepayIndexController',function ($scope, API,$stateParams,addCart,$state,$ionicSlideBoxDelegate,$timeout,$ionicPopup) {
    API.getStoreList(function(err,result){
        $scope.list = result;
    });

    API.getSignAds({
        sign : 'ddf'
    },function(err,result){
        $scope.SignAds = result.data.SignAds;
        $ionicSlideBoxDelegate.$getByHandle('SignAds').update();
        $ionicSlideBoxDelegate.$getByHandle('SignAds').loop(true);
    });

    $scope.alert = function(msg){
        var confirm = $ionicPopup.confirm({
            title: '消息提示',
            template : '敬请期待',
            buttons: [
                {
                    text: '<b>确定</b>',
                    type: 'button-positive'
                }
            ]
        });
    };
    $scope.$emit('hideNav',false);
    
});
module.controller('StorepayMayaController',function ($scope, API,$stateParams,addCart,$state,$ionicSlideBoxDelegate,$timeout,$ionicPopup) {
    var id = '57d11b815222d26ff3a9b699';
    $scope.id = id;
    API.getStorepayInfo({
        id : id
    },function(err,result){
        $scope.content = result;
    });
    $scope.$emit('hideNav',false);

});
module.controller('StorepayXyController',function ($scope, API,$stateParams,addCart,$state,$ionicSlideBoxDelegate,$timeout,$ionicPopup) {
    var id = '57d1177feaff7c6e18c03a0d';
    $scope.id = id;
    API.getStorepayInfo({
        id : id
    },function(err,result){
        $scope.content = result;
    });
    $scope.$emit('hideNav',false);

});


module.controller('StorepayPayController',function ($scope,API,$ionicModal,$ionicPopup,$state,$window,$base64,$stateParams,$localStorage ) {
    //$location.path('/order/index');
    $scope.$emit('hideNav',true);
    var id = $stateParams.id;
    $scope.id = id;
    API.getStorepayInfo({
        id : id
    },function(err,result){
        $scope.content = result;
    });
    API.getStore({
        id : id
    },function(err,result){
        // if(result.data && result.data[0]){
        //     result.data[0].checked = true;
        //     $scope.checked = result.data[0];
        // }
        $scope.stores = result.data;
        $scope.checked = {};
        $scope.checked.store_address = '请选择支付门店';

    });
    $scope.s_coupon = '使用优惠券';
    $scope.cash_num = {};
    if(id == '57d1177feaff7c6e18c03a0d'){
        $scope.is_xy = 1;
        API.getCouponList({
            status : 1
        },function(err,coupon_res){
            $scope.couponlist = coupon_res.data.list;

            for(var i in $scope.couponlist){
                $scope.couponlist[i].checked = false;
            }
            $ionicModal.fromTemplateUrl('tpl/huodong/select_coupon.html', {
                scope: $scope,
                animation: 'slide-in-left'
            }).then(function(modal) {
                $scope.coupon_check = modal;
            });
        });

        $scope.open_coupon = function($event){
            if($scope.pay_type.wepay != 'active'){
                $scope.alert('只有微信支付可以使用优惠券');
            }else if(parseFloat($scope.cash_num.price) < 200 || isNaN(parseFloat($scope.cash_num.price))) {
                $scope.alert('消费金额必须大于200可以使用优惠券');
            }else{
                $scope.coupon_check.show($event);
            }
        };
        $scope.check_coupon_close = function($event){
            $scope.coupon_check.hide($event);
        };
        $scope.check_coupon = '';
        $scope.checkcoupon = function(x){
            for(var i in $scope.couponlist){
                if($scope.couponlist[i]._id == x._id && $scope.check_coupon != x._id){
                    $scope.couponlist[i].checked = true;
                }else{
                    $scope.couponlist[i].checked = false;
                }
            }
            if($scope.check_coupon != x._id){
                $scope.check_coupon = x._id;
                $scope.s_coupon = '已选择满200-60的优惠券';
            }else{
                $scope.s_coupon = '使用优惠券';
                $scope.check_coupon = '';
            }
            $scope.count_cash();
            $scope.check_coupon_close();
        };

    }
    $scope.select_pay = function(action){
        if(action == 'wepay'){
            $scope.pay_type.wepay = 'active';
            $scope.pay_type.yue = '';
            $scope.pay_type.vip = '';
            $scope.pay_type.pay_type = '1';
            $scope.count_cash();
        }else if(action == 'yue'){
            $scope.pay_type.wepay = '';
            $scope.pay_type.yue = 'active';
            $scope.pay_type.vip = '';
            $scope.pay_type.pay_type = '2';
            $scope.s_coupon = '使用优惠券';
            $scope.check_coupon = '';
            for(var i in $scope.couponlist){
                $scope.couponlist[i].checked = false;
            }
            $scope.count_cash();
        }else{
            $scope.pay_type.wepay = '';
            $scope.pay_type.yue = '';
            $scope.pay_type.vip = 'active';
            $scope.pay_type.pay_type = '3';
            $scope.s_coupon = '使用优惠券';
            $scope.check_coupon = '';
            for(var i in $scope.couponlist){
                $scope.couponlist[i].checked = false;
            }
            $scope.count_cash();
        }
    };
    API.vipPrivilege(function(err,result){
        var vipinfo = result.data;
        vipinfo.vip_discount = (parseFloat(vipinfo.vip_discount)*10).toFixed(1);
        $scope.vipinfo = vipinfo;

        var zhe = $scope.content.discount[($scope.vipinfo.user_type)];
        $scope.zhe = zhe;
        $scope.show_zhe = parseFloat(zhe*10).toFixed(2);
    });
    API.getPostal(function(err,result){
        $scope.postal = result;
    });
    $ionicModal.fromTemplateUrl('tpl/vip/select_address.html', {
        scope: $scope,
        animation: 'slide-in-left'
    }).then(function(modal) {
        $scope.address_list = modal;
    });
    $scope.pay_price = 0;
    $scope.pay_price = $scope.pay_price.toFixed(2);
    $scope.open_address = function($event){
        $scope.address_list.show($event);
    };
    $scope.count_cash = function(){
        if($scope.pay_type.pay_type == 1){
            if(parseFloat($scope.cash_num.price) >= 0){
                $scope.pay_price = parseFloat($scope.cash_num.price).toFixed(2);
                if($scope.check_coupon != ''){
                    $scope.pay_price = $scope.pay_price-60;
                }
            }
        }else if($scope.pay_type.pay_type == 2){
            if(parseFloat($scope.cash_num.price) >= 0){
                $scope.pay_price = parseFloat($scope.cash_num.price).toFixed(2);
            }
        }else{
            if(parseFloat($scope.cash_num.price) >= 0){
                $scope.pay_price = parseFloat($scope.cash_num.price)*parseFloat($scope.zhe);

                $scope.pay_price = $scope.pay_price.toFixed(2);
            }
        }
    };
    $scope.check_address_close = function($event){

        $scope.address_list.hide($event);
    };

    $scope.alert = function(msg){
        var confirm = $ionicPopup.confirm({
            title: '消息提示',
            template : msg,
            buttons: [
                {
                    text: '<b>确定</b>',
                    type: 'button-positive'
                }
            ]
        });
    };
    $scope.pay_type = {};
    $scope.pay_type.pay_type = 3;
    $scope.pay_type.wepay = '';
    $scope.pay_type.yue = '';
    $scope.pay_type.vip = 'active';
    $scope.pay = function(){
        if(!$scope.checked._id){
            return $scope.alert('请选择支付门店');
        }
        if(parseFloat($scope.pay_price) <= 0){
            return $scope.alert('请输入正确的数字');
        }
        if($scope.pay_type.pay_type == 3){
            if(parseFloat($scope.pay_price) >= parseFloat($scope.vipinfo.vip_postal)){
                return $scope.alert('VIP余额不足');
            }
            pay_money = $scope.pay_price;
        }
        if($scope.pay_type.pay_type == 2){
            if(parseFloat($scope.pay_price) >= parseFloat($scope.postal.money)){
                return $scope.alert('余额不足');
            }
            pay_money = $scope.cash_num.price;
        }
        if($scope.pay_type.pay_type == 1 ){
            pay_money = $scope.cash_num.price;

        }
        if($scope.pay_type.pay_type != 1){
            var data = {
                pay_money : pay_money,
                pay_type : $scope.pay_type.pay_type,
                store_id : $scope.checked._id,
                need_money : $scope.cash_num.price,
            };
        }else{

            var data = {
                pay_money : pay_money,
                pay_type : $scope.pay_type.pay_type,
                store_id : $scope.checked._id,
                need_money : $scope.cash_num.price,
                coupon : $scope.check_coupon
            };
        }
        API.storePay(data,function(err,result){

            if(result.status == 200){
                if($scope.pay_type.pay_type == 1){
                    var wepay = result.data.wepay;
                    wepay.vip_cach = true;
                    var info = angular.toJson(wepay);
                    $window.location.href='http://www.hjfzqy.cn/#/pay?info='+$base64.encode(info);
                }else{
                    $scope.alert('支付成功');
                    $state.reload();
                }
            }else{
                $scope.alert('支付失败，余额不足');
            }
        });
        // var confirm = $ionicPopup.confirm({
        //     title: '选择支付方式',
        //     templateUrl: 'tpl/public/check_pay.html',
        //     okText : '确定',
        //     cancelText : '取消',
        //     scope : $scope
        // });
        // confirm.then(function(){
        //     var pay_money = 0;
        //
        // });

    };

    $scope.check_address = function(info){
        $scope.checked = info;
        for(i in $scope.stores){
            if(info._id == $scope.stores[i]._id){
                $scope.stores[i].checked = true;
            }else{
                $scope.stores[i].checked = false;
            }
        }

        $scope.address_list.hide();
    };
});

var isEmptyObject = function( obj ) {

    for ( var name in obj ) {
        return false;
    }
    return true;
};




